<template >
  <div class="no-close flex" id="app-nav-top-template" style="height: 100%;">
    <tools-page :need-back='true' title="安检" @back="back()"></tools-page>
    <div class="flex span" id="top-nav">
      <tabset v-ref:tabs :close="false" >
        <tab header="用户信息">
          <user-gasinfo :user='user'v-ref:user></user-gasinfo>
          <button type="button" class="btn btn-primary btn-block" @click="save()">保存</button>
        </tab>
        <tab header="设备检查">
          <equipment-inspection :user='user' v-ref:equipment></equipment-inspection>
        </tab>
        <tab header="隐患检查">
          <hiddentrouble-investigation :user='user' v-ref:hiddentrouble></hiddentrouble-investigation>
        </tab>
        <tab header="用户意见">
          <customer-opinions :user='user' v-ref:customer></customer-opinions>
        </tab>
      </tabset>
    </div>

  </div>
</template>

<script>
import co from 'co'
import Vue from 'vue'
let savedata = function*(self) {
      let back = yield self.$post( Vue.url +'logic/savecheck', {data: {flag: 1,userinfo:self.userinfo, equipment:self.equipment, hiddentrouble:self.hiddentrouble, customer:self.customer}},
      // let back = yield self.$post( 'rs/logic/savecheck', {data: {flag: 1,userinfo:self.userinfo, equipment:self.equipment, hiddentrouble:self.hiddentrouble, customer:self.customer}},
    { resolveMsg: null, rejectMsg: null })
    .then((res) => {
      self.$showMessage('保存数据成功')
    })
    .catch((ret)=>{
      let data = []
      //保存到手机内部，等有网络，进行保存
      HostApp.__callback__ = self.callback
      HostApp.__this__ = self
      HostApp.saveerrordata({userinfo:self.userinfo, equipment:self.equipment, hiddentrouble:self.hiddentrouble, customer:self.customer})
    })
}
export default {
  props: {
    user: {
      type: Object
    }
  },
  title: '顶部导航',
  data(){
    return {
      userinfo: {},
      equipment: {},
      hiddentrouble: {},
      customer: {}

    }
  },
  methods: {
    //头部事件回掉
    back () {
      this.$back()
    },
    save(){
      console.log('拿到的登陆人信息',Vue.orgs);
      this.userinfo = Object.assign({}, this.userinfo, this.$refs.user.model)
      this.equipment = Object.assign({}, this.equipment, this.$refs.equipment.equipmentinspection)
      this.hiddentrouble = Object.assign({}, this.hiddentrouble, this.$refs.hiddentrouble.hinvestigation)
      this.customer = Object.assign({}, this.customer, this.$refs.customer.userinfo)
      this.userinfo.oldid = `'${this.userinfo.id}'`
      this.userinfo.id =  `'${this.userinfo.id}'`
      this.userinfo.f_idnumber = this.userinfo.f_identity_id
      this.userinfo.f_checker = Vue.orgs[0].name

      this.userinfo.f_area = this.userinfo.f_area[0]
      this.userinfo.f_street = this.userinfo.f_street[0]
      this.userinfo.f_road = this.userinfo.f_road[0]
      this.userinfo.f_aroundmeter = this.userinfo.f_aroundmeter[0]
      this.userinfo.f_metertype = this.userinfo.f_metertype[0]
      this.userinfo.f_gaswatchbrand = this.userinfo.f_gaswatchbrand[0]
      window.alert(this.userinfo.f_area)
      if ( this.userinfo.f_area === '' || this.userinfo.f_area === 'null' || this.userinfo.f_area === null) {
        this.$showMessage('请选择区域')
        return
      }
      if ( this.userinfo.f_street === '' || this.userinfo.f_street === 'null' || this.userinfo.f_street === null) {
        this.$showMessage('请选择路段')
        return
      }
      if ( this.userinfo.f_road === '' || this.userinfo.f_road === 'null' || this.userinfo.f_road === null) {
        this.$showMessage('请选择街道')
        return
      }
      if ( this.userinfo.f_apartment === '' || this.userinfo.f_apartment === 'null' || this.userinfo.f_apartment === null) {
        this.$showMessage('请选择门牌号')
        return
      }
      if (this.user.f_usertype === '民用') {
        if ( this.userinfo.f_districtname === '' || this.userinfo.f_districtname === 'null' || this.userinfo.f_districtname === null) {
          this.$showMessage('请选择小区')
          return
        }
        if ( this.userinfo.f_cusdom === '' || this.userinfo.f_cusdom === 'null' || this.userinfo.f_cusdom === null) {
          this.$showMessage('请选择楼号')
          return
        }
        if ( this.userinfo.f_cusdy === '' || this.userinfo.f_cusdy === 'null' || this.userinfo.f_cusdy === null) {
          this.$showMessage('请选择单元')
          return
        }
        if ( this.userinfo.f_cusfloor === '' || this.userinfo.f_cusfloor === 'null' || this.userinfo.f_cusfloor === null) {
          this.$showMessage('请选择楼层')
          return
        }
      }
      this.userinfo.f_newaddress = this.userinfo.f_area+this.userinfo.f_street +this.userinfo.f_road +this.userinfo.f_districtname+
      this.userinfo.f_cusdom +'楼'+ this.userinfo.f_cusdy +'单元'+ this.userinfo.f_cusfloor+'层' + this.userinfo.f_apartment+'号'

      let gen = savedata(this)
      return co(gen)

    },
    // 错误数据保存回掉
    callback(flag) {
      if(!flag) {
          this.$showMessage('手机存储数据失败')
          return
      }
      HostApp.__callback__ = null
      HostApp.__this__ = null
      this.$showMessage('手机存储数据成功')
    }
  },

}
</script>

<style lang="less">
  #app-nav-top-template{
    .nav {
      > li {
        width: 25%;
        text-align: center;
        > a {
          padding: 10px 0px;
        }
      }
    }
  }

</style>
